// 小程序兼容的主题管理工具

// 主题配置
export const themeConfig = {
  default: {
    name: '默认主题',
    primary: '#3478f6',
    primaryLight: '#5a8ff8',
    primaryDark: '#2a5fd4',
    accent: '#ff6b35'
  },
  green: {
    name: '绿色主题',
    primary: '#52c41a',
    primaryLight: '#73d13d',
    primaryDark: '#389e0d',
    accent: '#ff7a45'
  },
  purple: {
    name: '紫色主题',
    primary: '#722ed1',
    primaryLight: '#9254de',
    primaryDark: '#531dab',
    accent: '#ff7875'
  },
  orange: {
    name: '橙色主题',
    primary: '#fa8c16',
    primaryLight: '#ffa940',
    primaryDark: '#d46b08',
    accent: '#ff4d4f'
  },
  red: {
    name: '红色主题',
    primary: '#C6282B',
    primaryLight: '#ff7875',
    primaryDark: '#cf1322',
    accent: '#fa8c16'
  },
  dark: {
    name: '深色主题',
    primary: '#1890ff',
    primaryLight: '#40a9ff',
    primaryDark: '#096dd9',
    accent: '#ff6b35'
  },
  enterprise: {
    name: '企业主题',
    primary: '#1890ff',
    primaryLight: '#40a9ff',
    primaryDark: '#096dd9',
    accent: '#52c41a'
  },
  tech: {
    name: '科技主题',
    primary: '#722ed1',
    primaryLight: '#9254de',
    primaryDark: '#531dab',
    accent: '#13c2c2'
  }
}

// 获取当前主题
export function getCurrentTheme() {
  try {
    // #ifdef MP-WEIXIN || MP-ALIPAY || MP-BAIDU || MP-TOUTIAO || MP-QQ
    const app = getApp()
    if (app && app.globalData && app.globalData.currentTheme) {
      return app.globalData.currentTheme
    }
    // #endif
    
    // 从本地存储获取
    const theme = uni.getStorageSync('currentTheme')
    if (theme && themeConfig[theme]) {
      return theme
    }
  } catch (error) {
    console.warn('获取当前主题失败:', error)
  }
  
  return 'red' // 默认红色主题
}

// 设置主题
export function setTheme(themeName) {
  if (!themeConfig[themeName]) {
    console.warn('主题不存在:', themeName)
    return false
  }
  
  try {
    // 保存到本地存储
    uni.setStorageSync('currentTheme', themeName)
    
    // #ifdef MP-WEIXIN || MP-ALIPAY || MP-BAIDU || MP-TOUTIAO || MP-QQ
    // 保存到全局数据
    const app = getApp()
    if (app && app.globalData) {
      app.globalData.currentTheme = themeName
      app.globalData.themeColors = themeConfig[themeName]
    }
    // #endif
    
    return true
  } catch (error) {
    console.error('设置主题失败:', error)
    return false
  }
}

// 初始化主题
export function initTheme() {
  try {
    const currentTheme = getCurrentTheme()
    
    // #ifdef MP-WEIXIN || MP-ALIPAY || MP-BAIDU || MP-TOUTIAO || MP-QQ
    const app = getApp()
    if (app && app.globalData) {
      app.globalData.currentTheme = currentTheme
      app.globalData.themeColors = themeConfig[currentTheme]
    }
    // #endif
    
    return currentTheme
  } catch (error) {
    console.warn('初始化主题失败:', error)
    return 'red'
  }
}

// 获取主题颜色值
export function getThemeColor(colorName) {
  try {
    const currentTheme = getCurrentTheme()
    const theme = themeConfig[currentTheme]
    if (theme) {
      switch (colorName) {
        case 'primary':
        case 'primary-color':
          return theme.primary
        case 'primary-light':
          return theme.primaryLight
        case 'primary-dark':
          return theme.primaryDark
        case 'accent':
        case 'accent-color':
          return theme.accent
        default:
          return theme.primary
      }
    }
  } catch (error) {
    console.warn('获取主题颜色失败:', error)
  }
  
  // 默认返回红色主题
  return '#C6282B'
}

// 获取所有主题列表
export function getThemeList() {
  return Object.keys(themeConfig).map(key => ({
    key,
    ...themeConfig[key]
  }))
}

// 获取当前主题配置
export function getCurrentThemeConfig() {
  const currentTheme = getCurrentTheme()
  return themeConfig[currentTheme] || themeConfig.red
}

// 主题颜色映射表（用于替换CSS变量）
export const themeColorMap = {
  '--primary-color': '#C6282B',
  '--primary-light': '#ff7875',
  '--primary-dark': '#cf1322',
  '--accent-color': '#fa8c16',
  '--bg-secondary': '#f7f8fa',
  '--text-primary': '#333333',
  '--text-secondary': '#666666',
  '--border-color': '#e8e8e8'
}

// 替换CSS变量为实际颜色值
export function replaceCssVariables(cssString) {
  let result = cssString
  Object.entries(themeColorMap).forEach(([variable, color]) => {
    result = result.replace(new RegExp(variable, 'g'), color)
  })
  return result
}

// 获取主题相关的样式类
export function getThemeClasses() {
  const currentTheme = getCurrentTheme()
  return {
    'theme-primary': `color: ${getThemeColor('primary')} !important;`,
    'theme-bg-primary': `background-color: ${getThemeColor('primary')} !important;`,
    'theme-border-primary': `border-color: ${getThemeColor('primary')} !important;`
  }
} 